﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tecnorama.KasaSatis
{
    public partial class UrunSatisFormu : Form
    {
        public UrunSatisFormu()
        {
            InitializeComponent();
        }
        LinqDataContext db = new LinqDataContext();
        private void UrunSatisFormu_Load(object sender, EventArgs e)
        {
            
            var sorgu = from x in db.Kategorilers select new{x.KategoriAdi} ;
            foreach (var item in sorgu)
            {
                cbkategori.Items.Add(item.KategoriAdi);
            }
            
        }

        private void cbkategori_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            cbmarka.Items.Clear();
            string a = cbkategori.Text;
            var sorgu1=from y in db.Kategorilers where y.KategoriAdi==a select new {y.KategoriID};

            foreach (var item in sorgu1)
            {
                var sorgu = from x in db.Uruns where x.KategoriId == item.KategoriID select new { x.UrunAdi };
            foreach (var item1 in sorgu)
            {
                comboBox1.Items.Add(item1.UrunAdi);

            }
                  
            }
   
        }

        private void txtTCkimlik_KeyPress(object sender, KeyPressEventArgs e)
        {
            lblad.Text = "";
            lbladres.Text = "";
            lblcepno.Text = "";
            lblev.Text = "";
            lblmail.Text = "";
            lblsirket.Text = "";
             if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
            {
                if (!(e.KeyChar == '\b'))
                {
                    e.Handled = false;
                }
            }
            else
            {
                //iptal edilmesi gereken yer
                e.Handled = true;
            }
             if (e.KeyChar == (char)Keys.Enter)
             {


             var sorgu = (from x in db.Musteri_Bilgileris where x.TCKimlikNo == (txtTCkimlik.Text) select x);


             foreach (var item in sorgu)
                 {
                     lblad.Text = item.MusteriAdi;
                     lblsirket.Text = item.SirketAdi;
                     lblcepno.Text = item.CepTelefonu;
                     lblev.Text = item.EvTelefonu;
                     lblmail.Text = item.Email;
                     lbladres.Text = item.Adresi;

                 }

             }
        
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbmarka.Items.Clear();
            var sorgu = from x in db.Uruns where x.UrunAdi == comboBox1.Text select new { x.MarkaID};
            foreach (var item in sorgu)
            {
                var sorgu1 = from y in db.Markalars where y.MarkaID == item.MarkaID select new {y.MarkaFirmaAdi };
                foreach (var item1 in sorgu1)
                {
                    cbmarka.Items.Add(item1.MarkaFirmaAdi);
                }
            }
        }

        private void cbmarka_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblbarkod.Text = "";
            lblurunadi.Text = "";
            lbluretimyil.Text = "";
            lblmarka.Text = "";
            lblKategori.Text = "";
            var sorgu1 = from x in db.Markalars where x.MarkaFirmaAdi == cbmarka.Text select new { x.MarkaID };
            foreach (var item1 in sorgu1)
            {
                var sorgu = from x in db.Uruns where x.MarkaID==item1.MarkaID select new{x.UrunBarkod,x.UretimYili,x.UrunAdi,x.UrunStok,x.UrunFiyat,x.KritikSeviye};
            foreach (var item in sorgu)
            {
                lblbarkod.Text = item.UrunBarkod;
                lblurunadi.Text = item.UrunAdi;
                lbluretimyil.Text =item.UretimYili.ToString();
                textBox1.Text = item.UrunStok.ToString();
                txtTL.Text = item.UrunFiyat;
               
            }
            }
            lblKategori.Text = cbkategori.Text;
            lblmarka.Text = cbmarka.Text;
            
        }

        private void UrunSatisFormu_FormClosed(object sender, FormClosedEventArgs e)
        {
            KasaAnaGiris frm = Application.OpenForms["KasaAnaGiris"] as KasaAnaGiris;
            frm.Show();
        }

        private void txtadet_KeyPress(object sender, KeyPressEventArgs e)
        {
              if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
            {
                if (!(e.KeyChar == '\b'))
                {
                    e.Handled = false;
                }
            }
            else
            {
                //iptal edilmesi gereken yer
                e.Handled = true;
            }
              if (e.KeyChar == (char)Keys.Enter)
              {

                  var sorgu1 = from x in db.Markalars where x.MarkaFirmaAdi == cbmarka.Text select new { x.MarkaID };
                  foreach (var item1 in sorgu1)
                  {
                      var sorgu = from x in db.Uruns where x.MarkaID == item1.MarkaID select new { x.KritikSeviye };
                      foreach (var item in sorgu)
                      {
                         
                          if (Convert.ToInt32(txtadet.Text) >= item.KritikSeviye)
                          {
                              MessageBox.Show("Kritik Seviyeden fazla miktar girdiniz..");
                              txtadet.Text = "";
                          }
                          else
                              MessageBox.Show("Girilen miktar uygundur");
                          //comment
                      }
                  }
              }
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void btnOnayla_Click(object sender, EventArgs e)
        {

        }
    }
}
